package xdu.lz.stage1_basic.chapter4_threadException;

//线程内部的异常异能被try不能throw,但是有些没有被try到的异常，我们可以通过一个
//API来获取线程内部的异常
public class ThreadException {

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int a = 1/0; // 假设并不知道这个会出现异常
        });

       /* @FunctionalInterface
        public interface UncaughtExceptionHandler {
            *//**
             * Method invoked when the given thread terminates due to the
             * given uncaught exception.
             * <p>Any exception thrown by this method will be ignored by the
             * Java Virtual Machine.
             * @param t the thread
             * @param e the exception
             *//*
            void uncaughtException(Thread t, Throwable e);
        }
        public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) {
            checkAccess();
            uncaughtExceptionHandler = eh;
        }*/
        t.setUncaughtExceptionHandler((thread,e)->{

            System.out.println("所属的线程:"+thread);
            System.out.println("错误信息:"+e);
        });

        t.start();

    }
}
